package cn.shyouchi.wuguanjia.android.ui.report;

import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.ui.attendance.report.StaffPostEntity;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class SelPostReportActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_posts)
    RecyclerView mRvPosts;

    AttendanceApi mAttendanceApi;
    List<StaffPostEntity.ResultMessageBean> staffPosts = new ArrayList<>();

    PostReportAdapter mPostReportAdapter;

    @Override
    public int initContentView() {
        return R.layout.activity_sel_post_report;
    }

    @Override
    public void initInjector() {
        mAttendanceApi = new AttendanceApi(mContext);

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        mPostReportAdapter = new PostReportAdapter(staffPosts);
        mRvPosts.setAdapter(mPostReportAdapter);
        mRvPosts.setLayoutManager(new LinearLayoutManager(mContext));


        fetchSignPostList();


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchSignPostList() {
        mAttendanceApi.getSignPostList()
                .subscribeOn(Schedulers.io())
                .map(new Func1<StaffPostEntity, List<StaffPostEntity.ResultMessageBean>>() {
                    @Override
                    public List<StaffPostEntity.ResultMessageBean> call(StaffPostEntity staffPostEntity) {
                        return staffPostEntity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<StaffPostEntity.ResultMessageBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        showAlert(e.getMessage());
                    }

                    @Override
                    public void onNext(List<StaffPostEntity.ResultMessageBean> resultMessageBeen) {
                        staffPosts.clear();
                        staffPosts.addAll(resultMessageBeen);
                        mPostReportAdapter.notifyDataSetChanged();
                    }
                });
    }

//
//    private synchronized void showSelPostDialog() {
//
//        List<String> postName = new ArrayList<>();
//
//        if (staffPosts.isEmpty()) {
//            ToastUtil.showToast("岗位为空");
//            return;
//        }
//
//        for (StaffPostEntity.ResultMessageBean b : staffPosts) {
//            postName.add(b.getPost());
//        }
//
//        String[] ranks = postName.toArray(new String[postName.size()]);
//
//        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
//        TextView tvTitle = (TextView) getLayoutInflater().inflate(R.layout.dialog_title, null);
//        tvTitle.setText("选择报岗");
//        builder.setCustomTitle(tvTitle);
//        builder.setItems(ranks, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Intent intent = new Intent(mContext, StaffReportActivity.class);
//                intent.putExtra("post", staffPosts.get(which));
//                startActivity(intent);
//                dialog.dismiss();
//            }
//        });
//        builder.setNegativeButton("取消", null);
//        builder.create().show();
//    }


    private void showAlert(String msg) {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mContext, android.app.AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        builder.create().show();
    }

}
